package pers.qianyu.month_202101.date_20210111;

/**
 * 733. 图像渲染
 * https://leetcode-cn.com/problems/flood-fill/
 *
 * @author mizzle rain
 * @date 2021-01-11 14:53
 */
public class FloodFill {
    int[][] dxy = {{-1, 0}, {0, -1}, {1, 0}, {0, 1}};

    public int[][] floodFill(int[][] image, int sr, int sc, int newColor) {
        int h = image.length;
        if (h == 0) return image;
        int w = image[0].length;
        if (w == 0) return image;
        int oldColor = image[sr][sc];
        if (oldColor == newColor) return image;
        image[sr][sc] = newColor;
        for (int[] d : dxy) {
            int x = d[0] + sr, y = d[1] + sc;
            if (x >= 0 && x < h && y >= 0 && y < w && image[x][y] == oldColor) {
                floodFill(image, x, y, newColor);
            }
        }
        return image;
    }
}
